//
//  StatusCellTopView.swift
//  sinaBlog
//
//  Created by tianxiangyang on 15/12/18.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class StatusCellTopView: UIView {
    
    //动态topView的底部约束
    var topViewBottomConstrain:Constraint?

    //MARK:模型
    var status:BlogStatus?{
        didSet{
        
            //给子控件赋值
        iconView.sd_setImageWithURL(status?.user?.iconImageURL)
        
            titleLabel.text = status?.user?.name
            
            mbrankImage.image = status?.user?.mbrankImage
            
        verified_type_image.image = status?.user?.verified_type_image
        
            timeLabel.text = status?.created_at
            
            diviceLabel.text = status?.source
            
            contentLabel.text = status?.text

            //给pictureView传模型数据
             pictureView.status = status
            
            //根据模型数据动态修改topView的底部约束(有无配图视图)
            
            //卸载约束
            
            topViewBottomConstrain?.uninstall()
            
            if let urls = status?.pictureURLs where urls.count > 0{
            //有配图,修改约束.
            
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.topViewBottomConstrain =  make.bottom.equalTo(pictureView.snp_bottom).constraint
                    
                })
            
            }else{
            //无配图,修改约束
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.topViewBottomConstrain =  make.bottom.equalTo(contentLabel.snp_bottom).constraint
                    
                })
            
            }
        
        }
    
    }
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
//        backgroundColor = UIColor(white: 0.9, alpha: 1)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:设置界面
    private func setupUI(){
    
    //设置背景颜色
//      backgroundColor = UIColor.blueColor()
    //添加子控件
        
        //微博分割视图
        addSubview(sepView)
        
        sepView.backgroundColor = UIColor(white: 0.8, alpha: 1)
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.height.equalTo(5)
        }
        
        
        //头像
        addSubview(iconView)
      

        iconView.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(sepView).offset(statusMargin)
            make.size.equalTo(CGSize(width:iconImageWidth, height: iconImageWidth))
 
        }
        
        //切圆

        iconView.layer.cornerRadius = iconImageWidth * 0.5
        iconView.layer.masksToBounds = true
        
        
        
        
        //昵称
        addSubview(titleLabel)
        
        titleLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(statusMargin)
        }
        //微博等级图片
        addSubview(mbrankImage)
        
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(titleLabel.snp_top)
        make.left.equalTo(titleLabel.snp_right).offset(statusMargin)
        }
        //认证图片
        addSubview(verified_type_image)
        
        verified_type_image.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        //添加发送时间
        addSubview(timeLabel)
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(iconView.snp_right).offset(statusMargin)
        }
        //添加发送设备
        addSubview(diviceLabel)
        
        diviceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(statusMargin)
        }
        
        //添加正文
        addSubview(contentLabel)
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(statusMargin)
            make.left.equalTo(iconView.snp_left)
        }
        
        //添加图片视图
        
        addSubview(pictureView)

        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self).offset(statusMargin)
            make.height.equalTo(100)
            make.width.equalTo(100)
            make.top.equalTo(contentLabel.snp_bottom)
        }
        
        //设置自身约束
        self.snp_makeConstraints { (make) -> Void in
         self.topViewBottomConstrain =  make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
        
        
    }
    
    //MARK:懒加载子控件
    
    //微博分割视图
    lazy var sepView:UIView = UIView()
    
    //头像
    lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_image_profile"))
    //昵称
    lazy var titleLabel:UILabel = UILabel(title: "liuyunxin", text_Color: themeColor(), fontSize: 14)
    //等级图片
    lazy var mbrankImage:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //认证图片
    lazy var verified_type_image:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //发微博时间
    lazy var timeLabel:UILabel = UILabel(title: "14:00", text_Color: themeColor(), fontSize: 10)
    //发送设备
    lazy var diviceLabel:UILabel = UILabel(title: "iPhone 7", text_Color: UIColor.blackColor(), fontSize: 10)
    
    //正文
    
    lazy var contentLabel:UILabel = UILabel(title: "你是猴子请来的逗比吗你是猴子请来的逗比吗你是猴子请来的逗比吗你是猴子请来的逗比吗你", text_Color: UIColor.darkGrayColor(), fontSize: 14, maxWidth: ScreenWidth-2*statusMargin)
    
    //pictureView
    lazy var pictureView:StatusPictureView = StatusPictureView()
    
    
    
    

}
